const app = getApp()

Page({
  data: {
    // 用户分页的属性
    totalPage: 1,
    page: 1,
    videoList: [],

    screenWidth: 350,
    serverUrl: "",
    searchContent: "",
    
  },

  onLoad: function (params) {
    var me = this;
    var screenWidth = wx.getSystemInfoSync().screenWidth;

    me.setData({
      screenWidth: screenWidth,
    });

    var searchContent = params.search;
    var isSaveRecord = params.isSaveRecord;
    if (isSaveRecord == null || isSaveRecord == ''|| isSaveRecord == undefined){
      isSaveRecord = 0;
    }

    me.setData({
      searchContent:searchContent
    })

    // 获取当前的分页页数
    var page = me.data.page;
    me.getAllVideoList(page, isSaveRecord)
  },

  getAllVideoList:function(page, isSaveRecord){
    var me = this;
    var serverUrl = app.serverUrl;

    wx.showLoading({
      title: '请等待，加载中~',
    })

    var searchContent = me.data.searchContent;
    wx.request({
      url: serverUrl + "/video/showAll?page=" + page + "&isSaveRecord=" + isSaveRecord,
      method:"POST",
      data:{
        videoDesc:searchContent
      },
      header:{
        "content-type": "application/json"
      },
      success:function(res){
          wx.hideLoading();
          wx.hideNavigationBarLoading()
          wx.stopPullDownRefresh() // 停止当前页面下拉刷新
          console.log(res.data)
          // 判断当前page是否是第一页，如果是第一页，那么要将videoList为空
          if (page == 1){
            me.setData({
              videoList:[]
            })
          }

          var videoList = res.data.data.rows;//后端的list
          var newVideoList = me.data.videoList; //现有的list

          me.setData({
            videoList:newVideoList.concat(videoList),
            page: page,
            totalPage: res.data.data.total,
            serverUrl:serverUrl
          }) 
      }
    })
  },

  /**
   * 上拉刷新
   */
  onReachBottom:function(){
    var me = this;
    var currentPage = me.data.page;
    var totalPage = me.data.totalPage;
    //判断当前的页数和总页数是否相等，如果相等则无需查询
    if (currentPage == totalPage){
      wx.showToast({
        title: '已经没有视频啦~',
        icon: "none"
      })
      return;
    }
    //分页查询
    var page = currentPage + 1;
    me.getAllVideoList(page, 0)
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function(){
    wx.showNavigationBarLoading()
    this.getAllVideoList(1, 0)
  },

  showVideoInfo:function(e){
    console.log(e)
    var me = this;
    var videoList = me.data.videoList;
    var arrindex = e.target.dataset.arrindex;

    var videoInfo =JSON.stringify(videoList[arrindex]);//videoList[arrindex];为一个对象，无法传入到下一个页面需要转化为String

    wx.navigateTo({
      url: '../videoInfo/videoInfo?videoInfo=' + videoInfo,
    })

  }

})
